package com.example.administrator.thread_test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import bean.book;
import bean.bookAdapter;
import bean.book_info;
import bean.infoAdapter;

public class detailActivity extends Activity {
//    TextView tv_detail;
    Handler handler = new Handler();
    TextView info_name;
    TextView info_author;
    TextView info_publish;
    TextView book_info;
    ArrayList<book_info> temp;
    ListView li ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);
        li = (ListView)findViewById(R.id.list_info);
        info_name = (TextView)findViewById(R.id.td_bookname);
        info_author = (TextView)findViewById(R.id.td_author);
        info_publish = (TextView)findViewById(R.id.td_publish);
        book_info = (TextView)findViewById(R.id.book_info);
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        String id=bundle.getString("id");
//        tv_detail = (TextView)findViewById(R.id.tv_detail);
        final String url = "http://opac.lib.wust.edu.cn:8080/opac/"+id;
//        tv_detail.setText(url);
        new Thread(new Runnable() {
            @Override
            public void run() {
                final String r= getData(url);

                Document doc = Jsoup.parse(r);
                Elements elems = doc.getElementsByClass("booklist");
                int size= elems.size();
                final String book_name = elems.get(0).text().split(": ")[1].split("\\/")[0];
                final String book_author = elems.get(0).text().split(": ")[1].split("\\/")[1];
                final String book_publish = elems.get(1).text().split(": ")[1];
//                 book_info3 = "";
//                Log.v("his",book_info2);
//                String res = doc.getElementById("intro").text();
                String info = "";
                for (Element elem: elems) {
                    String name = elem.text();
//                    Log.v("his",name);
                    CharSequence cs1 = "提要文摘附注:";
                    if(name.contains(cs1))
                    {
                        info = name;
                        Log.v("his",info);
                        break;
                    }
                }
                Elements tables = doc.getElementById("item").select("tr");
//                String tab = doc.getElementById("item").getElementsByTag().text();
//                Log.v("cnm",tab);
                temp = new ArrayList<>();
                for(int i = 1;i<tables.size();i++)
                {
                    Elements tds = tables.get(i).select("td");
                     String info_index = tds.get(0).text();
                     String info_num= tds.get(1).text();
                     String info_address= tds.get(2).text();
                     String info_status= tds.get(3).text();
                    bean.book_info temp2 = new book_info(info_index,info_num,info_address,info_status);
                    temp.add(temp2);
                }

                Log.v("cnm",temp.toString());
                final String book_info2 = info;

//                final String r_tran ="";



                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        info_author.setText(book_author);
                        info_name.setText(book_name);
                        info_publish.setText(book_publish);
                        book_info.setText(book_info2);

                        infoAdapter adapter = new infoAdapter(detailActivity.this,R.layout.info_list,temp);
                        li.setAdapter(adapter);
                    }
                });
            }
        }).start();
//        String data = getData(url);
        Log.v("msg",url);
    }

    public String getData(String httpUrl)
    {
        String msg = "";
        try {
            URL url = new URL(httpUrl);
//创建HttpURLConnection对象
            HttpURLConnection httpURLConnection = (HttpURLConnection)
                    url.openConnection();

            httpURLConnection.setConnectTimeout(5000); //设置连接超时为5秒
            httpURLConnection.setRequestMethod("GET"); //设置请求方式(默认为get)
            httpURLConnection.setRequestProperty("Charset", "uft-8"); //设置uft-8字符集
            httpURLConnection.connect();
//获得服务器反馈状态信息
//200：表示成功完成(HTTP_OK)， 404：请求资源不存在(HTTP_NOT_FOUND)
            int code = httpURLConnection.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK) {
//接收服务器返回的信息（输入流）
                BufferedReader bufferedReader = new BufferedReader(
                        new InputStreamReader(httpURLConnection.getInputStream(),"utf-8"));
                String line = "";
                while ((line = bufferedReader.readLine()) != null) {
                    msg+=line;
                }
//关闭缓冲区和连接
                bufferedReader.close();
                httpURLConnection.disconnect();
            }
//            Log.v("ajax",msg);
//                return msg;
//复制前页get流程代码…
        } catch (IOException e) {
            e.printStackTrace();
        }

        return msg;
    }
}
